<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript">
        function onclickFun() {
            var id = document.getElementById("username");
            var value = id.value;
            var patt = /^\w{5,12}$/;
            if(patt.test(value)){
                alert("用户名合法");
            }else {
                alert("用户名不合法");

            }



        }

    </script>
</head>
<body>

<input type="text" id="username" />
<button onclick="onclickFun()">校验</button>

</body>
</html>